/**
 * DD_Packet.java
 *
 * @author Hagen Boehm
 */


package SSF.OS.OSPFv2;


import java.util.*;
import SSF.OS.ProtocolMessage;


// ========================= class SSF.OS.OSPFv2.DD_Packet ============================ //
/**
 * This class implements an OSPF Database Description packet. The following from RFC 2328 was
 * not included:
 * 1.) Version
 * 2.) Packet length
 * 3.) Checksum
 * 4.) Authentication 
 */
public class DD_Packet extends ProtocolMessage {

  public int interfaceMTU;
  public boolean[] options;
  public boolean initialize;
  public boolean more;
  public boolean isMaster;
  public int DD_seq_num;
  private Vector lsaHeaders;

  // ----------------------------- constructor DD_Packet ---------------------------- //

    public DD_Packet(int interfaceMTU, boolean[] options, boolean initialize, boolean more, 
                     boolean isMaster, int DD_seq_num) {
    this.interfaceMTU = interfaceMTU;
    this.options       = (boolean[])options.clone();
    this.initialize    = initialize;
    this.more          = more;
    this.isMaster      = isMaster;
    this.DD_seq_num    = DD_seq_num;
    this.lsaHeaders    = new Vector();
  }

  // ----------------------------- DD_Packet.carryPayload ---------------------------- //

  public void carryPayload(ProtocolMessage payload) {
    System.err.println("** Warning: DD_Packet can't take payload; dropping!");
  }

  // ------------------------------ DD_Packet.addLSAHeader -------------------------- //
  /** Add a new LSA Header to the list of LSA Headers in this DD Packet. */
  public void addLSAHeader(LSA lsa) {
    lsaHeaders.add(lsa);
  }

  // ------------------------------- DD_Packet.getHeaders --------------------------- //
  /** Return the Vector with the LSA Headers carried by this DD Packet. */
  public Vector getHeaders() {
    return lsaHeaders;
  }

  // ------------------------------ DD_PAcket.isEmpty ------------------------------- //
  /** check LSA_Headers on content. If it is empty return true, else false. */
  public boolean isEmpty() {
    return lsaHeaders.isEmpty();
  }

  // ------------------------------- DD_Packet.bytecount ---------------------------- //
  /** Return the total size (in bytes) of this DD Packet body. 
   *  All and only fields specified in RFC 2328 are included in calculation!!!
   */
  public int bytecount(){
    return header_bytecount();
  }

  // ---------------------------- DD_Packet.header_bytecount ------------------------- //
  /** Return the size (in bytes) of this header only, don't include a payload. 
   *  Database Description Packets don't have payload.
   */
  public int header_bytecount(){
    return (8 + LSA.headerByteCount() * lsaHeaders.size());
  }

  // --------------------------------- DD_Packet.copy -------------------------------- //
  /** Return an exact copy of the Database Description Packet Content. */
  public ProtocolMessage copy() {
    DD_Packet dataDescCopy = new DD_Packet(interfaceMTU, options, initialize, more, isMaster, DD_seq_num);
    for (Enumeration lsas = lsaHeaders.elements(); lsas.hasMoreElements(); ) {
      dataDescCopy.addLSAHeader(((LSA)lsas.nextElement()).copyHeader());
    }
    return dataDescCopy;
  }

  // ------------------------------- DD_Packet.toString ------------------------------ //
  public String toString() {
    String s = "";
    s = interfaceMTU + " ";
    for (int i=0; i<8; i++) {
      s+= (new Boolean(options[i])).toString()+ " ";
    }
    s+= (new Boolean (initialize)).toString() + " " + (new Boolean(more)).toString() + " " + 
        (new Boolean(isMaster)).toString() + " " + DD_seq_num;
    for (Enumeration e = lsaHeaders.elements() ; e.hasMoreElements() ;) {
      s += " " + e.nextElement().toString();
    }  
    return s;
  }

} //end class DD_Packet




